package com.lw.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import org.apache.commons.codec.binary.Base64;

public class MD5Utils {

	/**
	 * @Description: 对字符串进行md5加密
	 */
	public static String getMD5Str(String strValue){
		MessageDigest md5 = null;
		try {
			md5 = MessageDigest.getInstance("MD5");
			byte[] bs = md5.digest(strValue.getBytes());
			String newstr = Base64.encodeBase64String(bs);
			return newstr;
		} catch (NoSuchAlgorithmException e) {
			//异常时就手动加密下，不抛出异常
			byte[] bs = strValue.getBytes();
			for (int i = 0; i < bs.length; i++) {
				bs[i] += 2;
			}
			String newstr = Base64.encodeBase64String(bs);
			newstr = "check_"+newstr;
			return newstr;
		}

	}

	public static void main(String[] args) {
		try {
			String md5 = getMD5Str("imooc");//kU8h64TG/bK2Y91vRT9lyg==
			System.out.println(Base64.encodeBase64String("imoock".getBytes()));

			System.out.println(md5);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
